#!/usr/bin/python3
# -*- coding:utf-8 -*-

import sys
from  PyQt5.QtWidgets import (QApplication,
                               QWidget,
                               QLabel,
                               QDesktopWidget,
                               QGroupBox,
                               QVBoxLayout,QColorDialog
                               )
from PyQt5.QtGui import QPainter, QPen, QBrush, QColor
from PyQt5 import QtCore
from PyQt5.QtCore import Qt, QRect

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowFlags(QtCore.Qt.FramelessWindowHint
                            | QtCore.Qt.Tool)  # 去掉标题栏
                                                # | QtCore.Qt.WindowStaysOnTopHint
        # self.setAttribute(QtCore.Qt.WA_TranslucentBackground)

        # self.showFullScreen()
        self.screen_size = QLabel(u'尺寸：', self)
        # 创建一个水平布局
        v_box_screen = QVBoxLayout()
        v_box_screen.addWidget(self.screen_size)
        # self.showFullScreen()

        # 创建一个组件组
        group_screen = QGroupBox('屏幕：', self)
        group_screen.setGeometry(0, 0, 120, 60)
        group_screen.setLayout(v_box_screen)

        self.window_position = QLabel(u'坐标：', self)
        self.window_size = QLabel(u'尺寸：', self)
        # 创建一个水平布局
        v_box_window = QVBoxLayout()
        v_box_window.addWidget(self.window_position)
        v_box_window.addWidget(self.window_size)

        # 创建一个组件组
        group_window = QGroupBox('窗口：', self)
        group_window.setGeometry(10, 70, 120, 80)
        group_window.setLayout(v_box_window)

        self.resize(300, 200)
        self.setWindowTitle('Simple Window')

        self.child_wins = []


        # self.setWindowFlags(Qt.FramelessWindowHint | Qt.Tool | Qt.WindowStaysOnTopHint);
        # // 去掉标题栏,去掉任务栏显示，窗口置顶

        # self.setWindowFlags(Qt.FramelessWindowHint|Qt.WindowStaysOnTopHint|Qt.Tool)


        self.information()

    def information(self):
        desktop = QDesktopWidget()
        screen_count = desktop.screenCount()
        # print(screen_count)
        # print('desktop:', desktop.width(), desktop.height())
        # self.setGeometry(-100,-100, desktop.width(), desktop.height())
        tx = ''
        for i in range(screen_count):
            cur_scr = desktop.screenGeometry(i)
            screen_width = cur_scr.width()
            screen_height = cur_scr.height()

            child = Child()
            child.setGeometry(cur_scr)
            child.showFullScreen()
            child.show()
            self.child_wins.append(child)
            if i != 0:
                tx += '\n'

            tx +=  u'尺寸[{}]：{}*{}'.format(i, str(screen_width),str(screen_height))
        print(tx)
        self.screen_size.setText(tx)

    def moveEvent(self, *args, **kwargs):
        """重写移动函数"""
        window_x = self.geometry().x()
        window_y = self.geometry().y()
        # self.window_position.setText(u'坐标：' + str(window_x) + u' * ' + str(window_y))

    def resizeEvent(self, *args, **kwargs):
        """重写改变尺寸函数函数"""
        window_width = self.geometry().width()
        window_height = self.geometry().height()
        # self.window_size.setText(u'尺寸：' + str(window_width) + u' * ' + str(window_height))

    def mouseReleaseEvent(self, event):
        if event.button()==Qt.LeftButton:
            exit(0)

class Child(QWidget):
    def __init__(self):
        super().__init__()
        self.setAttribute(QtCore.Qt.WA_TranslucentBackground)
        self.f_pose = None
        self.l_pose = None
        self.c_pose = None
        self.dirty = False
        self.pen = QPen()
        self.brush = QBrush() # Qt.SolidPattern
        self.color = QColor(0, 0, 0)
        # col = QColorDialog.getColor()

    def paintEvent(self, e):

        qp = QPainter()
        qp.begin(self)
        qp.setCompositionMode(QPainter.CompositionMode_DestinationOver)
        self.drawBrushes(qp)
        qp.end()

    def drawBrushes(self, qp):
        if self.f_pose is not None and self.l_pose is not None:
            self.brush.setStyle(Qt.Dense6Pattern)
            qp.setBrush(self.brush)
            self.pen.setColor(self.color)
            qp.setPen(self.pen)
            print('drawBrushes:', self.f_pose, self.l_pose)
            qp.drawRect(QRect(self.f_pose, self.l_pose))

            self.brush.setStyle(Qt.Dense6Pattern)
            qp.drawRect(QRect(self.f_pose, self.c_pose))

    def mouseReleaseEvent(self, event):
        if event.button()==Qt.RightButton:
            exit(0)
        elif event.button()==Qt.LeftButton:
            print('globle:',event.globalPos())
            self.dirty = False

    def mouseMoveEvent(self,event):

        # print('drawBrushes:', self.f_pose, self.l_pose, event.button()==Qt.LeftButton ,event.button(), Qt.LeftButton, self.dirty)
        if self.dirty:
            print('drawBrushes:', self.f_pose, self.l_pose, self.dirty)
            if self.l_pose is None:
                self.l_pose = event.pos()
            else:
                self.l_pose = self.c_pose
            self.c_pose = event.pos()

            self.update()

    def mousePressEvent(self,event):
        #鼠标按下时，获取鼠标的当前位置保存为上一次位置
        if event.button()==Qt.LeftButton:
            self.f_pose = event.pos()
            self.dirty = True

if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    # window.
    # window.show()
    sys.exit(app.exec_())
